package com.ydm.myapp001.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.ydm.myapp001.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Second002Activity extends AppCompatActivity {
    private static final String TAG = "YDMLogSecond002Activity";
    private GridView gView;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
    //图片id封装为一个数组
    private int[] iconArr = {R.drawable.test_grid_img001, R.drawable.test_grid_img002, R.drawable.test_grid_img003, R.drawable.test_grid_img004, R.drawable.test_grid_img005, R.drawable.test_grid_img006, R.drawable.test_grid_img007};
    private String[] iconNameArr = {"应用001", "应用002", "应用003", "应用004", "应用005", "应用006", "应用007"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second002);

        gView = (GridView) findViewById(R.id.gview);

        //获取数据
        getData();

        //新建适配器
        String[] from = {"image", "text"};
        int[] to = {R.id.image, R.id.text};
        sim_adapter = new SimpleAdapter(this, data_list, R.layout.item_gridview, from, to);
        //装载适配器
        gView.setAdapter(sim_adapter);

        //点击后的响应
        gView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, "onItemClick: 点击了" + iconNameArr[i]);
            }
        });
    }



    public List<Map<String, Object>> getData(){
        //新建List
        data_list = new ArrayList<Map<String, Object>>();

        for(int i=0;i<iconArr.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", iconArr[i]);
            map.put("text", iconNameArr[i]);
            data_list.add(map);
        }

        return data_list;
    }
}
